
package com.productmodule.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.UtilDAO;

import com.companymodule.bean.TbCompanyProductTypeMiddle;
import com.companymodule.dao.TbCompanyDAO;
import com.companymodule.dao.TbCompanyProductTypeMiddleDAO;
import com.productmodule.bean.TbProduct;
import com.productmodule.bean.TbProductPrice;
import com.productmodule.bean.TbProductType;
import com.productmodule.dao.TbInterfaceDAO;
import com.productmodule.dao.TbProductDAO;
import com.productmodule.dao.TbProductPriceDAO;
import com.productmodule.dao.TbProductTypeDAO;
import com.productmodule.dao.TbProductUnitDAO;
import com.usersmodule.bean.TbUsers;

public class TbProductServlet extends HttpServlet{
    public void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method")==null?"":request.getParameter("method");
        TbUsers tbUser = (TbUsers)request.getSession().getAttribute("Users");
        PrintWriter out = response.getWriter();
        if("add".equals(method)){
        	String productDate=request.getParameter("productDate");
        int productTypeId=Integer.parseInt(request.getParameter("productTypeId").equals("")?"0":request.getParameter("productTypeId"));
        String productName=request.getParameter("productName");
        String tbInterface[]=request.getParameterValues("interfaceId");
        int companyId=Integer.parseInt(request.getParameter("companyId"));
        int productUnitId=Integer.parseInt(request.getParameter("productUnitId"));
        String productMemo=request.getParameter("productMemo");
        int userId=tbUser.getUserId();
            if(new TbProductDAO().add(new TbProduct(productTypeId,productName,productMemo,userId))){
            	Hashtable<String,String> ht = new Hashtable<String,String>();
            	int maxProductId = new TbProductDAO().getMaxId();
            	if(tbInterface==null){//不选择界面
            		String productPrice = request.getParameter("productPrice");
            		if(new TbProductPriceDAO().add(new TbProductPrice(companyId,maxProductId,productUnitId,0,productPrice,userId,productDate,ht))){
            			request.getSession().setAttribute("result", "添加成功");
            		}else
            			 request.getSession().setAttribute("result", "添加失败");
            	}else{
            		
            		if(tbInterface.length>0){
            			for(String interfa:tbInterface){
            				ht.put(interfa, request.getParameter(interfa));
            			}
            		}
            		if(new TbProductPriceDAO().add(new TbProductPrice(companyId,maxProductId,productUnitId,0,"",userId,productDate,ht))){
            			request.getSession().setAttribute("result", "添加成功");
            		}else
            			 request.getSession().setAttribute("result", "添加失败");
            	}
            }else{            
            	request.getSession().setAttribute("result", "添加失败");
            }
            goList(request,response);
        }
        if("del".equals(method)){
            if(UtilDAO.delin("Tb_ProductPrice", "CompanyProductMiddleId", request.getParameter("delIds"))){ 
            	if(new TbProductPriceDAO().getProductByProductId(Integer.parseInt(request.getParameter("productId")))<=0){
            		if(UtilDAO.delin("Tb_Product","ProductId",request.getParameter("productId"))){
            			 request.getSession().setAttribute("result", "删除成功");
            		}else
            			 request.getSession().setAttribute("result", "删除失败");
            	}
            }else{            
            request.getSession().setAttribute("result", "删除失败");
            }
            goList(request,response);
        }
        if("edit".equals(method)){

        int productPriceMiddleId=Integer.parseInt(request.getParameter("productPriceMiddleId"));
        int productId=Integer.parseInt(request.getParameter("productId"));
        String productDate=request.getParameter("productDate");
        int productTypeId=Integer.parseInt(request.getParameter("productTypeId").equals("")?"0":request.getParameter("productTypeId"));
        String productName=request.getParameter("productName");
        String tbInterface[]=request.getParameterValues("interfaceId");
       System.out.println("tbInterface==="+tbInterface+"productDate="+productDate);
        int companyId=Integer.parseInt(request.getParameter("companyId"));
        int productUnitId=Integer.parseInt(request.getParameter("productUnitId"));
        String productMemo=request.getParameter("productMemo");
        int userId=tbUser.getUserId();
        String productPrice = "";
        	
        	
        if(new TbProductDAO().edit(new TbProduct(productId,productTypeId,productName,productMemo,userId))){
        	String condition=" Tb_ProductPrice where ProductDate='"+productDate+"' and ProductId="+productId;
        	if(new TbProductPriceDAO().del(condition)){
        		Hashtable<String,String> ht = new Hashtable<String,String>();
            	int maxProductId = new TbProductDAO().getMaxId();
            	if(tbInterface==null){//不选择界面
            		productPrice = request.getParameter("productPrice");
            		if(new TbProductPriceDAO().add(new TbProductPrice(companyId,maxProductId,productUnitId,0,productPrice,userId,productDate,ht))){
            			request.getSession().setAttribute("result", "修改成功");
            		}else
            			 request.getSession().setAttribute("result", "修改失败");
            	}else{
            		
            		if(tbInterface.length>0){
            			for(String interfa:tbInterface){
            				ht.put(interfa, request.getParameter(interfa));
            			}
            		}
            		if(new TbProductPriceDAO().add(new TbProductPrice(companyId,maxProductId,productUnitId,0,"",userId,productDate,ht))){
            			request.getSession().setAttribute("result", "修改成功");
            		}else
            			 request.getSession().setAttribute("result", "修改失败");
            	}
        	}
        
        }else{            
        	request.getSession().setAttribute("result", "修改失败");
        }
            goList(request,response);
        }
        if ("list".equals(method))goList(request, response);
        if ("listById".equals(method)) {
        	request.getSession().setAttribute("tbCompanyList", new TbCompanyDAO().getTbCompanyAll());
            request.getSession().setAttribute("tbInterfaceList", new TbInterfaceDAO().getTbInterfaceAll());
            request.getSession().setAttribute("tbProductUnitList", new TbProductUnitDAO().getTbProductUnitAll());
            Map<Integer,String> map = new TbProductPriceDAO().getProductPrice(new TbProductPrice(Integer.parseInt(request.getParameter("productId")),request.getParameter("productDate")));
            request.getSession().setAttribute("hm",map);
            request.getSession().setAttribute("tbProductPrice",new TbProductPriceDAO().getTbProductPriceByCompanyProductMiddleId(Integer.parseInt(request.getParameter("productPriceMiddleId"))));
            response.sendRedirect("ProductModule/TbProductOperate.jsp");
    	}
        if("showadd".equals(method)){
            request.getSession().removeAttribute("hm");
            request.getSession().removeAttribute("tbProductPrice");
            request.getSession().setAttribute("tbCompanyList", new TbCompanyDAO().getTbCompanyAll());
            request.getSession().setAttribute("tbInterfaceList", new TbInterfaceDAO().getTbInterfaceAll());
            request.getSession().setAttribute("tbProductUnitList", new TbProductUnitDAO().getTbProductUnitAll());
            response.sendRedirect("ProductModule/TbProductOperate.jsp");
        }
        if("getProductTypeByCompanyId".equals(method)){
        	System.out.println("come heree !!!!!!");
        	List<TbCompanyProductTypeMiddle> mlist=new TbCompanyProductTypeMiddleDAO().getTbCompanyProductTypeMiddleByCompanyId(Integer.parseInt(request.getParameter("companyId")));
        	String mstr="";
        	String allProductTypeIds = "";
        	
        	for (TbCompanyProductTypeMiddle m : mlist) {
        		 String sb = new TbProductTypeDAO(). getProductType(m.getTbProductType().getProductTypeId(),null);
        		 allProductTypeIds+=sb;
			}
        	
        	if(!allProductTypeIds.equals("")){
        		List<TbProductType> lastProductList = new TbProductTypeDAO(). getProductTypeByProductTypeId(allProductTypeIds.substring(0, allProductTypeIds.length()-1));
        		if(lastProductList!=null&&lastProductList.size()>0){
        			for(TbProductType pr:lastProductList){
        				if(mstr.equals("")){
        					mstr+=pr.getProductTypeId()+":"+pr.getParentTypeId()+":"+pr.getProductTypeName();
        				}else
        					mstr+=","+pr.getProductTypeId()+":"+pr.getParentTypeId()+":"+pr.getProductTypeName();
        			}
        		}
        	}
        	System.out.println("mstr==="+mstr);
        	response.getWriter().print(mstr);
        	
        }
        if("getParProductTypeByCompanyId".equals(method)){//根据公司ID得到一级产品类型
        	List<TbCompanyProductTypeMiddle> mlist=new TbCompanyProductTypeMiddleDAO().getProductTypeMiddleByIsMaxAndCompanyId(0,Integer.parseInt(request.getParameter("companyId")));
        	String mstr="";
        	for (TbCompanyProductTypeMiddle m : mlist) {
        		mstr="".equals(mstr)?m.getProductTypeId()+":"+(m.getTbProductType()!=null?m.getTbProductType().getProductTypeName():"无"):mstr+","+m.getProductTypeId()+":"+m.getTbProductType().getProductTypeName();
			}  
        	response.getWriter().write(mstr);
        }
        if("getProductByProductTypeId".equals(method)){
        	String productTypeId = request.getParameter("productTypeId");
        	request.getSession().setAttribute("tbProductList", new TbProductDAO().getProductByProductTypeId(Integer.parseInt(productTypeId)));
        	response.sendRedirect("ProductModule/getProduct.jsp");
        	
        }
        if("getProductByCompanyId".equals(method)){//get products by companyId
        	List<TbProduct> productList = new TbProductDAO().getProductByCompanyId(Integer.parseInt(request.getParameter("companyId")));
        	String result = "";
        	if(productList!=null&&productList.size()>0){
        		for(TbProduct product:productList){
        			if(result.equals("")){
        				result+=product.getProductId()+":"+product.getProductName();
        			}else
        			result+=","+product.getProductId()+":"+product.getProductName();
        		}
        	}
        	System.out.print("result==="+result);
        	response.getWriter().print(result);
        }
        //get products by ProductTypeId
        if("getProductByProductId".equals(method)){
        	List<TbProduct> productList = new TbProductDAO().getProductByProductTypeId(Integer.parseInt(request.getParameter("productTypeId")));
        	String result = "";
        	if(productList!=null&&productList.size()>0){
        		for(TbProduct product:productList){
        			if(result.equals("")){
        				result+=product.getProductId()+":"+product.getProductName();
        			}else
        			result+=","+product.getProductId()+":"+product.getProductName();
        		}
        	}
        	System.out.print("result==="+result);
        	response.getWriter().print(result);
        }
        if("getProductInfoByProductId".equals(method)){//get the info of product by productId
        	List<TbProductPrice> productPriceList = new TbProductPriceDAO().getTbProductPriceByProductId(Integer.parseInt(request.getParameter("productId")));
        	String result="";
        	if(productPriceList!=null&&productPriceList.size()>0){
        		for(TbProductPrice productPrice:productPriceList){
        			if(result.equals("")){
        				result+=new TbInterfaceDAO().getTbInterfaceByInterfaceId(productPrice.getInterfaceId()).getInterfaceName()+":"+productPrice.getProductPirce()+":"+new TbProductUnitDAO().getTbProductUnitByProductUnitId(productPrice.getProductUnitId()).getProductUnitName();
        			}else
        				result+=","+new TbInterfaceDAO().getTbInterfaceByInterfaceId(productPrice.getInterfaceId()).getInterfaceName()+":"+productPrice.getProductPirce()+":"+new TbProductUnitDAO().getTbProductUnitByProductUnitId(productPrice.getProductUnitId()).getProductUnitName();
        		}
        	}
        	response.getWriter().println(result);
        }
    }
    public void goList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	   request.getSession(true).setAttribute("tbProductPriceList",new TbProductPriceDAO().getTbProductPriceAll());
           response.sendRedirect("ProductModule/TbProductList.jsp");
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }
}
